<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\PayTradeRebate;
use backend\models\PayRebateUsertake;
use yii\db\Query;

/**
 * PayTradeRebateSearch represents the model behind the search form about `backend\models\PayTradeRebate`.
 */
class PayTradeRebateSearch extends PayTradeRebate
{
    public $end_time;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uid', 'level', 'rebate_uid', 'state' ], 'integer'],
            [['trade_no','tel','create_time','end_time'], 'safe'],
            [['money', 'rebate_money', 'rebate_rate'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    
    public function search($params)
    {
        $query1 = PayTradeRebate::find()
        ->select(['uid','tel','rebate_money AS show_money','create_time AS take_time','create_time AS apply_time','memo'])
        ->join('LEFT JOIN', 'user_info', 'user_info.id = pay_trade_rebate.uid')
        ->where(['>=','uid','2000']);
        $query2 = PayRebateUsertake::find()
        ->select(['uid','tel','-1*`take_money` AS show_money','take_time','apply_time','memo'])
        ->join('LEFT JOIN', 'user_info', 'user_info.id = pay_rebate_usertake.uid');
        $query= $query1->union($query2);
        $query = PayTradeRebate::find()->select(['uid','tel','show_money','take_time','apply_time','memo'])->from(['q'=>$query]);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
            'sort' => [
                'defaultOrder' => [
                    'uid' => SORT_ASC,
                ]
            ],
        ]);
        $dataProvider->sort->attributes['uid'] = [
            'asc' => ['uid' => SORT_ASC],
            'desc' => ['uid' => SORT_DESC],
        ];
        $this->load($params);
      
        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'uid' => $this->uid,
            'tel' => $this->tel
        ]);

        $query->andFilterWhere(['like', 'tel', $this->tel])
              ->andFilterWhere(['LIKE', 'uid', $this->uid])
            
              ;
      if(strtotime($this->create_time)>0){
          $query->andFilterWhere(['>=','apply_time',strtotime($this->create_time)]);
        }
        if(strtotime($this->end_time)>0&&strtotime($this->end_time)>strtotime($this->create_time)){
            $query->andFilterWhere(['<=','apply_time',strtotime($this->end_time)]);
        }
//         $query->union[0]['query']
//             ->andFilterWhere(['like', 'tel', $this->tel])
//             ->andFilterWhere(['LIKE', 'uid', $this->uid]);

        return $dataProvider;
    }
   
    /**
     * 待审核
     * @param   $params
     * @return \yii\data\ActiveDataProvider
     */
    public function auditSearch($params){
        $query = PayRebateUsertake::find()
        ->select(['uid','pay_rebate_usertake.id','tel','take_money' ,'take_time','apply_time','memo'])
        ->join('LEFT JOIN', 'user_info', 'user_info.id = pay_rebate_usertake.uid') 
        ->where(['=','state','0']);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
            'sort' => [
                'defaultOrder' => [
                    'uid' => SORT_ASC,
                ]
            ],
        ]);
        $dataProvider->sort->attributes['uid'] = [
            'asc' => ['uid' => SORT_ASC],
            'desc' => ['uid' => SORT_DESC],
        ];
        $this->load($params);
        
        if (!$this->validate()) {
            return $dataProvider;
        }
        
        $query->andFilterWhere([
            'uid' => $this->uid,
            'tel' => $this->tel
        ]);
        $query->andFilterWhere(['like', 'tel', $this->tel])
        ->andFilterWhere(['LIKE', 'uid', $this->uid])
        ;
        if(strtotime($this->create_time)>0){
            $query->andFilterWhere(['>=','apply_time',strtotime($this->create_time)]);
        }
        if(strtotime($this->end_time)>0&&strtotime($this->end_time)>strtotime($this->create_time)){
            $query->andFilterWhere(['<=','apply_time' ,strtotime($this->end_time."+1 day")]);
        }
        //         $query->union[0]['query']
        //             ->andFilterWhere(['like', 'tel', $this->tel])
        //             ->andFilterWhere(['LIKE', 'uid', $this->uid]);
        return $dataProvider;
    }

    /**
     *  打款
     * @param   $params
     */
    public function makemoneySearch($params){
        $query = PayRebateUsertake::find()
        ->select(['uid','state','pay_rebate_usertake.id','tel','take_money','take_time','apply_time','memo'])
        ->join('LEFT JOIN', 'user_info', 'user_info.id = pay_rebate_usertake.uid') 
        ->where("state=1 or state=2") ;
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
           'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],  
        ]);
        $dataProvider->sort->attributes['uid'] = [
            'asc' => ['uid' => SORT_ASC],
            'desc' => ['uid' => SORT_DESC],
        ];
        $this->load($params);
        if (!$this->validate()) {
            return $dataProvider;
        }
        $query->andFilterWhere([
            'uid' => $this->uid,
            'tel' => $this->tel
        ]);
        $query->andFilterWhere(['like', 'tel', $this->tel])
        ->andFilterWhere(['LIKE', 'uid', $this->uid])
        ;
        return $dataProvider;
    }
        
}
